﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="BooleanSettingDescriptorTests.cs" company="Radical Research Ltd">
//   Copyright (c) Radical Research Ltd. The MIT License (MIT)
// </copyright>
// <summary>
//   
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace RadicalResearch.MiniState.Tests
{
    using System;

    using NUnit.Framework;

    using RadicalResearch.MiniState;
    using RadicalResearch.MiniState.Descriptors;

    using Rhino.Mocks;

    /// <summary>
    /// Unit tests for BooleanSettingDescriptor
    /// </summary>
    [TestFixture]
    public class BooleanSettingDescriptorTests
    {
        /// <summary>Mock repository</summary>
        private MockRepository mocks;

        /// <summary>value collection</summary>
        private IValueCollection values;

        /// <summary>
        /// Fixtures the set up.
        /// </summary>
        [TestFixtureSetUp]
        public void FixtureSetUp()
        {
            this.mocks = new MockRepository();
        }

        /// <summary>
        /// Set up a single test.
        /// </summary>
        [SetUp]
        public void SetUp()
        {
            this.values = this.mocks.StrictMock<IValueCollection>();
        }

        /// <summary>
        /// Tears down.
        /// </summary>
        [TearDown]
        public void TearDown()
        {
            this.mocks.VerifyAll();
        }

        /// <summary>
        /// Booleans the setting descriptor constructor.
        /// </summary>
        [Test]
        public void BooleanSettingDescriptorConstructor()
        {
            this.mocks.ReplayAll();

            BooleanSettingDescriptor booleanSettingDescriptor = new BooleanSettingDescriptor("valueKey", 0, BooleanSettingDescriptor.BitValueSize.SingleBit);
            Assert.IsFalse(booleanSettingDescriptor.IsValidated);
            Assert.AreEqual("0valueKey", booleanSettingDescriptor.ValueKey);
        }

        /// <summary>
        /// Read a single bit value.
        /// </summary>
        [Test]
        public void BooleanSettingDescriptorSingleBitRead()
        {
            Expect.Call(this.values["0valueKey"]).Return("1");

            this.mocks.ReplayAll();

            BooleanSettingDescriptor booleanSettingDescriptor = new BooleanSettingDescriptor(
                "valueKey", 0, BooleanSettingDescriptor.BitValueSize.SingleBit);

            Assert.IsTrue((bool)booleanSettingDescriptor.ReadValue(this.values));
        }

        /// <summary>
        /// Read a single bit from a 32 bit value.
        /// </summary>
        [Test]
        public void BooleanSettingDescriptor32BitRead()
        {
            Expect.Call(this.values["0valueKey"]).Return("400");
            this.mocks.ReplayAll();

            BooleanSettingDescriptor booleanSettingDescriptor = new BooleanSettingDescriptor("valueKey", 10, BooleanSettingDescriptor.BitValueSize.ThirtyTwoBits);

            Assert.IsTrue((bool)booleanSettingDescriptor.ReadValue(this.values));
        }

        /// <summary>
        /// Write a single bit to a 32 bit value.
        /// </summary>
        [Test]
        public void BooleanSettingDescriptor32BitWrite()
        {
            Expect.Call(this.values["0valueKey"]).Return("10");
            Expect.Call(this.values["0valueKey"] = "410");

            this.mocks.ReplayAll();

            BooleanSettingDescriptor booleanSettingDescriptor = new BooleanSettingDescriptor("valueKey", 10, BooleanSettingDescriptor.BitValueSize.ThirtyTwoBits);

            booleanSettingDescriptor.WriteValue(this.values, true);
        }

        /// <summary>
        /// Write a single bit.
        /// </summary>
        [Test]
        public void BooleanSettingDescriptorSingleBitWrite()
        {
            Expect.Call(this.values["0valueKey"]).Return("0");
            Expect.Call(this.values["0valueKey"] = "1");

            this.mocks.ReplayAll();

            BooleanSettingDescriptor booleanSettingDescriptor = new BooleanSettingDescriptor("valueKey", 0, BooleanSettingDescriptor.BitValueSize.SingleBit);

            booleanSettingDescriptor.WriteValue(this.values, true);
        }

        /// <summary>
        /// Setting the bit index bigger then the size throws ArgumentOutOfRange exception.
        /// </summary>
        [Test]
        public void BooleanSettingDescriptorIndexTooBig()
        {
            this.mocks.ReplayAll();

            Assert.Throws(
                typeof(ArgumentOutOfRangeException),
                delegate { new BooleanSettingDescriptor("valueKey", 8, BooleanSettingDescriptor.BitValueSize.EightBits); });
        }
    }
}
